//
//  WBNewFeatureView.m
//  TransWB
//
//  Created by 孝飞王 on 2017/1/17.
//  Copyright © 2017年 孝飞王. All rights reserved.
//

#import "WBNewFeatureView.h"

@interface WBNewFeatureView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *baseScroView;
@property (weak, nonatomic) IBOutlet UIButton *clickBtn;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;



@end


@implementation WBNewFeatureView

+(WBNewFeatureView *)newFeatureView{
    
    UINib *nib = [UINib nibWithNibName:@"WBNewFeatureView" bundle:nil];
    WBNewFeatureView *welcomeView = [nib instantiateWithOwner:nil options:nil][0];
    welcomeView.frame = [UIScreen mainScreen].bounds;
    
    return welcomeView;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    NSArray *imageArr  = @[@"new_feature_1",@"new_feature_2",@"new_feature_3",@"new_feature_4"];
    
    self.baseScroView.contentSize = CGSizeMake(Screen_Width * (imageArr.count + 1) , Screen_Height);
    self.baseScroView.pagingEnabled = YES;
    self.baseScroView.showsVerticalScrollIndicator = NO;
    self.baseScroView.showsHorizontalScrollIndicator = NO;
    self.baseScroView.bounces = NO;
    self.baseScroView.delegate = self;
    
    for (NSInteger i = 0; i < imageArr.count; i++)
        [self.baseScroView addSubview:[self addImage:imageArr[i] index:i] ];
    
    self.pageControl.numberOfPages = imageArr.count;
    [self.baseScroView bringSubviewToFront:self.pageControl];
    self.clickBtn.hidden = YES;
    [self.clickBtn addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
    
}

- (UIImageView*)addImage:(NSString*)imageString index:(NSInteger)index{
    
    CGRect rect = [UIScreen mainScreen].bounds ;
    UIImageView *imageView  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageString]];
    imageView.frame  = CGRectOffset(rect, index * Screen_Width, 0);
    return imageView;
}


#pragma mark - UIScrolViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / Screen_Width ;
    self.pageControl.currentPage = index;
    
    if (index == scrollView.subviews.count - 1)
        [self removeSelf];
    self.clickBtn.hidden = index != scrollView.subviews.count - 2;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.clickBtn.hidden = YES;
    
    NSInteger index = scrollView.contentOffset.x / Screen_Width  + 0.5;
    self.pageControl.currentPage = index;
    
    self.pageControl.hidden = index == scrollView.subviews.count - 1;

}

- (void)removeSelf{
    [self removeFromSuperview];
}

  @end
